package com.ByteDance.service.impl;

import com.ByteDance.model.po.Message;
import com.ByteDance.mapper.MessageMapper;
import com.ByteDance.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.bytedeco.flycapture.FlyCapture2.TimeStamp;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 聊天记录表 服务实现类
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-06
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Override
    public Message sendMsg(int fromUserId, Integer toUserId, Integer actionType, String content) {
        if (actionType == 1) {
            Message message = new Message();
            message.setFromUserId(fromUserId);
            message.setToUserId(toUserId);
            message.setContent(content);
            message.setMessageTime(new Timestamp(System.currentTimeMillis()));
            baseMapper.insert(message);
            return message;
        }
        //actionType不合法
        return null;
    }

    @Override
    public List<Message> getMsgList(int fromUserId, Integer toUserId, String preMsgTime) {
        List<Message> list = lambdaQuery().eq(Message::getFromUserId, fromUserId)
                .eq(Message::getToUserId, toUserId)
                .le(Message::getMessageTime,preMsgTime)
                .list();

        List<Message> list1 = lambdaQuery().eq(Message::getFromUserId, toUserId)
                .eq(Message::getToUserId, fromUserId)
                .le(Message::getMessageTime,preMsgTime)
                .list();

        list.addAll(list1);
        return list;
    }
}
